<?php
namespace Framework\Library\Validator;

use Phalcon\Validation;
use Phalcon\Validation\Message;
use Framework\Library\PhoneNumber;
use Phalcon\Validation\Validator;
use Phalcon\Validation\ValidatorInterface;

class MobileValidator extends Validator implements ValidatorInterface
{
    public function validate(Validation $validation, $attribute)
    {
        $phoneNumber = $validation->getValue($attribute);

        if (($formattedPhoneNumber = PhoneNumber::getFormattedMobileNumber($phoneNumber)) === false) {
            $validation->appendMessage(new Message('Mobile phone format error', $attribute, 'Mobile'));

            return false;
        }
        return true;
    }
}
